<?php

namespace App\Http\Controllers\Manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Model\Notice;

class NoticeController extends Controller
{
    /**
     * 公告列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function noticeList() {
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
        $obj = new Notice();
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $notice = $obj->Paginate(20);
        if(empty($page)){
            $count = $obj->count();
        }

        return view('manage.notice.noticeList',[
            'notice'=>$notice,
            'count'=>$count,
            'orderBy'=>$orderBy,
            'active1'=>9,
            'active2'=>1
        ]);
    }

    /**
     *公告启禁用
     * @return array
     */
    public function noticeStatus() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 1) {
            $del = Notice::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = Notice::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

    /**
     * 载入添加页
     * @return
     */
    public function noticeAdd() {
        return view('manage.notice.noticeAdd',['active1'=>9,'active2'=>2]);
    }

    /**
     * 添加公告
     * @return array
     */
    public function create() {
        $title = input::get('title');
        $rank = input::get('rank');
        $issuer = input::get('issuer');
        $content = input::get('content');
        if(empty($title) || empty($content)) {
            return [
                'code' => 102,
                'msg' => '缺少元素!'
            ];
        }
        $list = Notice::where('title',$title)->first();
        if($list) {
            return [
                'code' => 103,
                'msg' => '该标题已存在!'
            ];
        }
        $map = Notice::insert([
            'title' => $title,
            'rank' => $rank,
            'issuer' => $issuer,
            'content' => $content,
        ]);
        if($map) {
            return [
                'code' => 100,
                'msg' => '添加成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' =>'添加失败!'
            ];
        }
    }

    /**
     * 获取公告ID
     * @param $id
     */
    public function noticeEdit($id) {
        $notice = Notice::where('id',$id)->first();
        return view('manage.notice.update',['notice'=>$notice,'active1'=>9,'active2'=>1]);
    }

    /**
     * 更新公告内容
     * @return array
     */
    public function update() {
        $id = input::get('id');
        $data['title'] = input::get('title');
        $data['rank'] = input::get('rank');
        $data['issuer'] = input::get('issuer');
        $data['content'] = input::get('content');
        if(empty($data['title']) || empty($data['content'])) {
            return [
                'code' => 102,
                'msg' => '缺少元素!'
            ];
        }
        $update = Notice::where('id',$id)->update($data);
        if($update) {
            return [
                'code' => 100,
                'msg' => '修改成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' =>'修改失败!'
            ];
        }
    }

    /**
     * 删除公告
     * @return array
     */
    public function noticeDelete(){
        $id = Input::get('id');
        $delete = Notice::where('id',$id)->delete();
        if($delete){
            return [
                'code'=>100,
                'msg'=>'删除成功！'
            ];
        }else{
            return [
                'code'=>101,
                'msg'=>'删除失败！'
            ];
        }
    }
}
